package cn.luoshaocheng.system.controller;

import cn.luoshaocheng.basic.annotation.PreAuthorize;
import cn.luoshaocheng.basic.util.PageList;
import cn.luoshaocheng.basic.util.jsonResult;
import cn.luoshaocheng.org.domain.Department;
import cn.luoshaocheng.system.domain.Menu;
import cn.luoshaocheng.system.domain.Systemdictionarydetail;
import cn.luoshaocheng.system.query.MenuQuery;
import cn.luoshaocheng.system.query.SystemdictionarydetailQuery;
import cn.luoshaocheng.system.service.IMenuService;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //controller层  返回的是json格式数据
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private IMenuService iMenuService;


    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    public List<Menu> loadAll() {
        return iMenuService.loadAll();
    }

    /**
     * 查询一个
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Menu loadById(@PathVariable("id") Long id) {
        return iMenuService.loadById(id);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public jsonResult deleteById(@PathVariable("id") Long id) {
        try {
            iMenuService.delete(id);
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  jsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * @param
     * @return
     */
    @PutMapping
    public jsonResult addAndUpdate(@RequestBody Menu Menu) {
        try {
            if (Menu.getId() == null) {
                iMenuService.save(Menu);
            } else {
                iMenuService.update(Menu);
            }
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  jsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页数据
     *
     * @param
     * @return
     */
    @PostMapping
    public PageList<Menu> queryPage(@RequestBody MenuQuery menuQuery) {
        return iMenuService.queryPage(menuQuery);

    }

    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping
    public jsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            iMenuService.patchDel(ids);
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  jsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * 查询部门树
     *
     * @return
     */
    @GetMapping("/menuTree")
    @ApiOperation(value = "获取部门树-无限极接口")
    //@PreAuthorize(name = "部门树",sn = "department:deptTree") //value后面用来做权限校验
    public List<Menu> menuTree() {
        return iMenuService.menuTree();
    }

}

